package com.sdx.console.meet.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.ibeetl.admin.core.annotation.Function;
import com.ibeetl.admin.core.entity.CoreUser;
import com.ibeetl.admin.core.service.CoreDictService;
import com.ibeetl.admin.core.service.CorePlatformService;
import com.ibeetl.admin.core.util.DateUtil;
import com.ibeetl.admin.core.util.ValidateConfig;
import com.ibeetl.admin.core.web.AuthorizedBaesController;
import com.ibeetl.admin.core.web.JsonResult;
import com.sdx.common.utils.JacksonUtils;
import com.sdx.common.utils.Utils;
import com.sdx.console.customer.entity.CustomerInfo;
import com.sdx.console.customer.service.CustomerInfoService;
import com.sdx.console.meet.entity.MeetPrice;
import com.sdx.console.meet.service.MeetPriceService;
import com.sdx.console.person.service.CustomerExtInfoService;
import org.apache.commons.lang3.StringUtils;
import org.beetl.sql.core.engine.PageQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;


/**
 * MeetPrice 接口
 */
@RequestMapping("/meet/meetPrice")
@Controller
public class MeetPriceController extends AuthorizedBaesController{
	private static final Logger log = LoggerFactory.getLogger(MeetPriceController.class);
    private static final String MODEL = "/meet/meetPrice";
    @Resource
    private CoreDictService coreDictService;

    @Resource
	private MeetPriceService meetPriceService;

    @Resource
    private CustomerInfoService customerInfoService;

    @Resource
    private CorePlatformService platformService;

    @Resource
    private CustomerExtInfoService customerExtInfoService;


	/**
	 * 初始化页面需要的列表数据
	 */
	private void setPageParam(ModelAndView view,HttpServletRequest request) {

	    //服务类型
       List serviceTypeList = coreDictService.findAllByType("SERVICE_TYPE");
       view.addObject("serviceTypeList",serviceTypeList);

       //服务老师
        Integer accessType = platformService.getAccessType(request);
        view.addObject("accessType",accessType);
        Integer orgId = null;
        if(accessType<6){
            CoreUser user = getLoginUser(request);
            orgId = user.getOrgId().intValue();
        }
        List<CustomerInfo> customerInfoList = customerExtInfoService.selectTeacher(orgId);
        view.addObject("customerInfoList",customerInfoList);


	}

    /* 页面 */

    @GetMapping("/index.do")
    @Function("meet.meetPrice.query")
    @ResponseBody
    public ModelAndView index(HttpServletRequest request) {
        ModelAndView view = new ModelAndView(MODEL+"/index.html") ;
		setPageParam(view,request);
        return view;
    }

    @GetMapping("/edit.do")
    @Function("meet.meetPrice.edit")
    @ResponseBody
    public ModelAndView edit(HttpServletRequest request,Integer serviceId) {
        ModelAndView view = new ModelAndView(MODEL+"/add.html");
        MeetPrice meetPrice = meetPriceService.queryById(serviceId);
        view.addObject("meetPrice", meetPrice);
		setPageParam(view,request);
		try {
			view.addObject("meetPriceJson", JacksonUtils.toJsonString(meetPrice,JacksonUtils.DF1));
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
        return view;
    }

    @GetMapping("/add.do")
    @Function("meet.meetPrice.add")
    @ResponseBody
    public ModelAndView add(HttpServletRequest request) {
        ModelAndView view = new ModelAndView(MODEL+"/add.html");
		setPageParam(view,request);
        return view;
    }

    /* ajax json */

    @PostMapping("/list.json")
    @Function("meet.meetPrice.query")
    @ResponseBody
    public JsonResult<PageQuery> list(MeetPrice condtion,HttpServletRequest request)
    {
        Integer accessType = platformService.getAccessType(request);
        CoreUser user = getLoginUser(request);
        if (accessType.intValue()==1) {
            String customerId = customerInfoService.getCustomerId(user.getId().intValue());
            if(StringUtils.isBlank(customerId)){
                customerId = "66666";
            }
            condtion.setCustomerId(customerId);
        }
        if(accessType.intValue()== 3 ){
            condtion.setOrgId(user.getOrgId());
        }
        PageQuery page = condtion.getPageQuery();
        meetPriceService.queryByCondition(page);
        return JsonResult.success(page);
    }

    private JsonResult save(boolean isNew,MeetPrice meetPrice,HttpServletRequest request
	) {
    	JsonResult result = JsonResult.success();
		String dir = DateUtil.now("yyyyMMdd");
    	String subPath = "/images/cover/"+dir+"/";
		String bucketName = "sdxcomm";
		CoreUser user = getLoginUser(request);

    	if (isNew) {
			meetPrice.setCreateTime(new Date());
			meetPrice.setUpdateTime(new Date());
			meetPriceService.save(meetPrice);
		} else {
			meetPrice.setUpdateTime(new Date());
			meetPriceService.updateTemplate(meetPrice);
		}
		return result;
	}
    @PostMapping("/add.json")
    @Function("meet.meetPrice.add")
    @ResponseBody
    public JsonResult add(@Validated(ValidateConfig.ADD.class)MeetPrice meetPrice,HttpServletRequest request)
    {
        return save(true,meetPrice,request);
    }

    @PostMapping("/update.json")
    @Function("meet.meetPrice.edit")
    @ResponseBody
    public JsonResult<String> update(@Validated(ValidateConfig.UPDATE.class)  MeetPrice meetPrice,HttpServletRequest request
		) {
		return save(false,meetPrice,request);
    }

    @GetMapping("/view.json")
    @Function("meet.meetPrice.query")
    @ResponseBody
    public JsonResult<MeetPrice>queryInfo(Integer serviceId) {
        MeetPrice meetPrice = meetPriceService.queryById( serviceId);
        return  JsonResult.success(meetPrice);
    }

    @PostMapping("/delete.json")
    @Function("meet.meetPrice.del")
    @ResponseBody
    public JsonResult delete(String ids) {
		List<String> idList = Utils.strToList(ids,",");
        meetPriceService.batchDelMeetPrice(idList);
        return JsonResult.success();
    }

}
